<template>
  <el-dialog
    v-if="visible"
    :title="info.title"
    :visible.sync="visible"
    append-to-body
    center
    width="760px"
    :before-close="handleCancel"
  >
    <el-form
      ref="formDate"
      label-suffix=":"
      label-width="120px"
      :rules="rules"
      :model="form"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="合同编号" prop="pur">
            <PurchaseSelectObject v-model="form.pur" :id="form.pur_id" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item style="height: 37px; overflow: hidden" label="项目名称">
            <span v-if="purchase.id">{{ purchase.pro_name }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="订单明细" prop="detailed">
            <el-input v-model="form.detailed" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item style="height: 37px; overflow: hidden" label="PO单号">
            <span v-if="purchase.id">{{ purchase.pro_id }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="签订公司" prop="company">
            <el-select v-model="form.company" size="small" style="width: 100%">
              <el-option
                v-for="item in companys"
                :key="item"
                :label="item"
                :value="item"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item style="height: 37px; overflow: hidden" label="支付对象">
            <span v-if="purchase.id">{{ purchase.sup_name }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="请款金额" prop="money">
            <el-input v-model="form.money" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            style="height: 37px; overflow: hidden"
            label="合同总金额"
          >
            <span v-if="purchase.id">{{
              purchase.price + purchase.additional - purchase.reduction
            }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="付款方式" prop="pay_type">
            <el-select v-model="form.pay_type" size="small" style="width: 100%">
              <el-option
                v-for="item in pay_types"
                :key="item"
                :label="item"
                :value="item"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="支付日期" prop="pay_time">
            <el-date-picker
              v-model="form.pay_time"
              type="date"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              size="small"
              clearable
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="附件上传" prop="files">
            <Upload v-model="form.files" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input
              v-model="form.remark"
              type="textarea"
              :rows="4"
              size="small"
              clearable
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
        <!-- <el-col class="mt10" :span="24">
          <el-form-item label="抄送" prop="ccs">
            <VerifyCC v-model="form.ccs" :ccs="ccs" />
          </el-form-item>
        </el-col> -->
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="fr mt10">
            <el-button size="small" @click="handleCancel()">取 消</el-button>
            <el-button type="primary" size="small" @click="handleBeforeTo()"
              >提 交</el-button
            >
          </div>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import PurchaseSelectObject from '@/myComponents/PurchaseSelectObject'
// import VerifyCC from '@/myComponents/VerifyCC'
import { pur as queryPur } from '@/api/purchase'
import Upload from '@/myComponents/Upload'
import svf from '../../util/svf'

export default {
  components: { Upload, PurchaseSelectObject },
  extends: svf,
  data() {
    const vnull = (rule, value, callback) => {
      if (JSON.stringify(value) === '{}') {
        callback(new Error(rule.message))
      } else {
        callback()
      }
    }
    return {
      approve_key: 'PROCESS_PURCHASE_EXPEND',
      purchase: {},
      rules: {
        pur: [
          {
            required: true,
            trigger: 'blur',
            message: '合同编号不可为空',
            validator: vnull,
          },
        ],
        detailed: [
          { required: true, trigger: 'blur', message: '订单明细不可为空' },
        ],
        company: [
          { required: true, trigger: 'blur', message: '公司名称不可为空' },
        ],
        money: [
          { required: true, trigger: 'blur', message: '请款金额不可为空' },
        ],
        pay_type: [
          { required: true, trigger: 'blur', message: '支付方式不可为空' },
        ],
        pay_time: [
          { required: true, trigger: 'blur', message: '支付日期不可为空' },
        ],
        files: [{ required: true, trigger: 'blur', message: '附件不可为空' }],
      },
    }
  },
  watch: {
    visible(val) {
      if (val) {
        if (this.info.type && this.info.type === 'again') {
          this.initApply(this.info.edit)
        } else {
          this.form = this.info.edit
        }
        this.$set(this.form, 'pur', {})
        this.$set(this, 'purchase', {})
        // this.queryCcs()
      }
    },
    'form.pur': {
      handler(n, o) {
        if (this.form.pur.key) {
          this.queryPur()
        }
      },
      deep: true,
    },
  },
  mounted() {},
  methods: {
    queryPur() {
      queryPur({ id: this.form.pur.key })
        .then((response) => {
          this.purchase = response.info
        })
        .catch((err) => {
          this.$message.error(err.msg)
        })
    },
    handleBeforeTo() {
      this.validate().then(() => {
        const e = {}
        e.files = this.form.files
        e.approve_key = this.approve_key
        e.des =
          '合同编号：LOPO' +
          this.purchase.id +
          '    支付对象：' +
          this.purchase.sup_name +
          '    请款金额：' +
          this.form.money +
          '    备注：' +
          this.form.remark
        e.ccs = this.form.ccs
        e.forms = JSON.stringify(
          this.createForms([
            ['PO单号', 'PO' + this.purchase.pro_id, '', ''],
            ['项目名称', this.purchase.pro_name, '', ''],
            ['签订公司', this.form.company, this.form.company, 'company'],
            ['合同编号', 'LOPO' + this.purchase.id, this.purchase.id, 'pur_id'],
            ['支付对象', this.purchase.sup_name, '', ''],
            ['订单明细', this.form.detailed, this.form.detailed, 'detailed'],
            ['请款金额', this.form.money, this.form.money, 'money'],
            [
              '合同总金额',
              this.purchase.price +
                this.purchase.additional -
                this.purchase.reduction,
              '',
              '',
            ],
            ['付款方式', this.form.pay_type, this.form.pay_type, 'pay_type'],
            ['支付日期', this.form.pay_time, this.form.pay_time, 'pay_time'],
            ['备注', this.form.remark, this.form.remark, 'remark'],
          ])
        )
        this.handleCreate(e)
      })
    },
  },
}
</script>

<style></style>
